﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.SellStaff
{
    public partial class guiSellStaffAccountManagerment : THSERPScreen
    {
        public guiSellStaffAccountManagerment()
        {
            InitializeComponent();

        }

        #region--Action--
        private void CreateUser()
        {
            int iUserID = 0;
            int iUserGroupID = Convert.ToInt32(fld_lkeADUserGroupID.EditValue);
            GELanguagesController objLanguageController = new GELanguagesController();
            ADUserGroupsController objUserGroupController = new ADUserGroupsController();
            ADUsersController objUserController = new ADUsersController();
            ADUsersInfo objUserInfo = new ADUsersInfo();
            if (iUserGroupID == 0)
            {
                MessageBox.Show("Please choose user group!", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (String.IsNullOrEmpty(fld_txtUserName.Text))
            {
                MessageBox.Show("User name can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (String.IsNullOrEmpty(fld_txtPassword.Text))
            {
                MessageBox.Show("Password can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (fld_txtPassword.Text != fld_txtConfirmPassword.Text)
            {
                MessageBox.Show("Password and confirm one do not match.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (IsExistUserName(fld_txtUserName.Text))
            {
                MessageBox.Show("The user name already exists. Please try with another!", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            objUserInfo.ADUserName = fld_txtUserName.Text;
            objUserInfo.ADPassword = fld_txtPassword.Text;
            objUserInfo.ADUserGroupID = iUserGroupID;
            objUserInfo.FK_HREmployeeID = ((BaseModule)this.Module).Toolbar.CurrentObjectID;
            iUserID = objUserController.CreateUser(objUserInfo);
            SellStaffEntities entity = (SellStaffEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            entity.ADUsersList.Invalidate(((BaseModuleERP)this.Module).Toolbar.CurrentObjectID);
            InvalidateGriControl();
            MessageBox.Show("Create account successfully.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void EditUser()
        {
            bool bChange = true;
            int iUserGroupID = Convert.ToInt32(fld_lkeADUserGroupID.EditValue);
            int iRowHandle = fld_dgvADUsers.FocusedRowHandle;
            if (iUserGroupID == 0)
            {
                MessageBox.Show("Please choose user group!", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (String.IsNullOrEmpty(fld_txtUserName.Text))
            {
                MessageBox.Show("User name can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (String.IsNullOrEmpty(fld_txtPassword.Text))
            {
                MessageBox.Show("Password can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (iRowHandle >= 0)
            {
                // objUserInfo.ADUserID = Convert.ToInt32(fld_txtUserNumber.Text);
                ADUsersInfo objADUsersInfo = (ADUsersInfo)fld_dgvADUsers.GetRow(iRowHandle);
                if (objADUsersInfo != null)
                {
                    if (!objADUsersInfo.ADUserName.Equals(fld_txtUserName.Text))
                    {
                        if (IsExistUserName(fld_txtUserName.Text))
                        {
                            MessageBox.Show("The user name already exists. Please try with another!", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }

                    if (objADUsersInfo.ADPassword == fld_txtPassword.Text)
                        bChange = false;
                    else
                    {
                        if (fld_txtPassword.Text != fld_txtConfirmPassword.Text)
                        {
                            MessageBox.Show("Password and confirm one do not match.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    objADUsersInfo.ADPassword = fld_txtPassword.Text;
                    objADUsersInfo.ADUserName = fld_txtUserName.Text;
                    objADUsersInfo.ADUserGroupID = iUserGroupID;
                    new ADUsersController().UpdateUser(objADUsersInfo, bChange);
                    SellStaffEntities entity = (SellStaffEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
                    entity.ADUsersList.Invalidate(((BaseModuleERP)this.Module).Toolbar.CurrentObjectID);
                    InvalidateGriControl();
                    MessageBox.Show("Change account information successfully.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        public void DeleteUsers()
        {
            SellStaffEntities entity = (SellStaffEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            int iRowHandle = fld_dgvADUsers.FocusedRowHandle;
            if (iRowHandle >= 0)
            {
                // objUserInfo.ADUserID = Convert.ToInt32(fld_txtUserNumber.Text);
                ADUsersInfo objADUsersInfo = (ADUsersInfo)fld_dgvADUsers.GetRow(iRowHandle);
                if (objADUsersInfo != null)
                {
                    if (objADUsersInfo.ADUserName.ToUpper().Contains("ADMIN"))
                    {
                        MessageBox.Show("Can not delete admin account.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    DialogResult dlgResults = MessageBox.Show("Do you want to delete this account?", "#Message#", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dlgResults == DialogResult.Yes)
                    {
                        new ADUsersController().DeleteObject(objADUsersInfo.ADUserID);
                        entity.ADUsersList.Invalidate(((BaseModuleERP)this.Module).Toolbar.CurrentObjectID);
                        InvalidateGriControl();
                    }
                }
            }
        }
        #endregion

        #region --Utilities--
        /// <summary>
        /// created by :Hieu.nguyen
        /// created date:March 10,2010
        /// Desc: refress data grid control 
        /// </summary>
        private void InvalidateGriControl()
        {
            SellStaffEntities entity = (SellStaffEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            fld_dgcADUsers.DataSource = entity.lstADUsers;
            fld_dgvADUsers.RefreshData();
        }
        /// <summary>
        /// created by :Hieu.nguyen
        /// created date:March 10,2010
        /// Desc: Check exsts user name
        /// </summary>
        /// 
        private bool IsExistUserName(String strUserName)
        {
            ADUsersInfo objADUsersInfo = (ADUsersInfo)new ADUsersController().GetObjectByName(strUserName);
            if (objADUsersInfo != null)
                return true;
            return false;
        }
        #endregion

        #region-- form even--
        /// <summary>
        /// created by :Hieu.nguyen
        /// created date:March 10,2010
        /// Desc: Button Add account
        /// </summary>
        private void fld_btnAdd_Click(object sender, EventArgs e)
        {
            CreateUser();
        }
        /// <summary>
        /// created by :Hieu.nguyen
        /// created date:March 10,2010
        /// Desc: Button Edit account
        /// </summary>
        private void fld_btnEdit_Click(object sender, EventArgs e)
        {
            EditUser();
        }

        /// <summary>
        /// created by :Hieu.nguyen
        /// created date:March 10,2010
        /// Desc: Button delete account
        /// </summary>
        private void fld_btnDelete_Click(object sender, EventArgs e)
        {
            DeleteUsers();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void guiUserManagerment_Load(object sender, EventArgs e)
        {
            DataSet dsADUserGroup = new ADUserGroupsController().GetAllObjects();
            List<ADUserGroupsInfo> lstADUserGroups = new List<ADUserGroupsInfo>();
            if (dsADUserGroup != null)
            {
                if (dsADUserGroup.Tables.Count > 0)
                {
                    //inital lookup datasource and set default SALE start group  
                    int iIndex = 0;
                    DataRow[] row = dsADUserGroup.Tables[0].Select("[ADUserGroupName]='SALE'");
                    if (row.Length > 0)
                    {
                        iIndex = dsADUserGroup.Tables[0].Rows.IndexOf(row[0]);
                    }
                    fld_lkeADUserGroupID.Properties.DataSource = dsADUserGroup.Tables[0];
                    fld_lkeADUserGroupID.ItemIndex = iIndex;
                    //inital repository resource
                    rplkeADUserGroups.DataSource = dsADUserGroup.Tables[0];
                }
            }
            InvalidateGriControl();

        }

        /// <summary>
        /// created by :Hieu.nguyen
        /// created date:March 10,2010
        /// Desc:even gridview click
        /// </summary>
        private void fld_dgvADUsers_Click(object sender, EventArgs e)
        {
            int iRowHandle = fld_dgvADUsers.FocusedRowHandle;
            if (iRowHandle >= 0)
            {
                ADUsersInfo objADUsersInfo = (ADUsersInfo)fld_dgvADUsers.GetRow(iRowHandle);
                if (objADUsersInfo != null)
                {
                    fld_txtUserName.Text = objADUsersInfo.ADUserName;
                    fld_txtPassword.Text = objADUsersInfo.ADPassword;
                    fld_lkeADUserGroupID.EditValue = objADUsersInfo.ADUserGroupID;
                }
            }
        }
        /// <summary>
        /// created by :Hieu.nguyen
        /// created date:March 10,2010
        /// Desc:even focused rowchanged click
        /// </summary>
        private void fld_dgvADUsers_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ADUsersInfo objADUsersInfo = (ADUsersInfo)fld_dgvADUsers.GetRow(e.FocusedRowHandle);
            if (objADUsersInfo != null)
            {
                fld_txtUserName.Text = objADUsersInfo.ADUserName;
                fld_txtPassword.Text = objADUsersInfo.ADPassword;
                fld_lkeADUserGroupID.EditValue = objADUsersInfo.ADUserGroupID;
            }
        }
        #endregion
    }
}